package com.eco.fanliapp.jd;

import com.alibaba.fastjson.JSON;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import jd.union.open.order.query.request.OrderReq;
import jd.union.open.order.query.request.UnionOpenOrderQueryRequest;
import jd.union.open.order.query.response.UnionOpenOrderQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sun.awt.windows.ThemeReader;


@Service
@Slf4j
public class JdOrderController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 拉取京东订单
     * @param pageNo
     * @param type
     * @param time  分
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/jd/order/orderdetails", method = RequestMethod.POST)
    public UnionOpenOrderQueryResponse getJDOrderDetails(@RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "3") Integer type,
                                                         String time, @RequestParam(defaultValue = "50000")int connectTimeout, @RequestParam(defaultValue = "50000")int readTimeout) throws  Exception{
        JdClient client=new DefaultJdClient(JDConfig.JD_URL,"",JDConfig.JD_APPKEY,JDConfig.JD_APPSECRET,connectTimeout,readTimeout);
        UnionOpenOrderQueryRequest req=new UnionOpenOrderQueryRequest();
        OrderReq orderReq=new OrderReq();
        orderReq.setPageNo(pageNo);
        orderReq.setPageSize(500);
        orderReq.setType(type);
        orderReq.setTime(time);
        req.setOrderReq(orderReq);
        try {
            UnionOpenOrderQueryResponse response=client.execute(req);
            return response;
        } catch (JdException e) {
            e.printStackTrace();
           // Thread.sleep(1000);
        }
        return null;
    }
//    @RequestMapping(value = "/jd/order/orderdetails", method = RequestMethod.POST)
//    public UnionOpenOrderQueryResponse getJDOrderDetailsRow(@RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "3") Integer type,
//                                                         String time, @RequestParam(defaultValue = "50000")int connectTimeout, @RequestParam(defaultValue = "50000")int readTimeout) throws  Exception{
//        JdClient client=new DefaultJdClientRow(JDConfig.JD_URL,"",JDConfig.JD_APPKEY,JDConfig.JD_APPSECRET);
//        UnionOpenOrderRowQueryRequest request=new UnionOpenOrderRowQueryRequest();
//        OrderRowReq orderReq=new OrderRowReq();
//        request.setOrderReq(orderReq);
//        UnionOpenOrderRowQueryResponse response=client.execute(request);
//        return null;
//    }

    public static void main(String args[]) throws Exception {
        JdOrderController orderController = new JdOrderController();
        UnionOpenOrderQueryResponse jdOrderDetails = orderController.getJDOrderDetails(1, 3, "202003201029",50000,50000);
        log.info(JSON.toJSONString(jdOrderDetails));

    }

}
